<?php
namespace Admin\Controller;
use Think\Controller;
class ListController extends Controller {

    private $_meta_title; // 页面标题
    private $_bread_crumb; //面包屑导航
    private $_top_button_list     = array(); // 顶部工具栏按钮组
    private $_search              = array(); // 搜索参数配置
    private $_table_column_list   = array(); // 表格标题字段
    private $_table_data_list     = array(); // 表格数据列表
    private $_table_data_page; // 表格数据分页
    private $_right_button_list = array(); // 表格右侧操作按钮组
    private $_template; // 模版

    protected function _initialize()
    {
        $this->_template = 'builder/list/template';
    }
    //设置页面标题
    public function setMetaTitle($meta_title)
    {
        $this->_meta_title = $meta_title;
        return $this;
    }
    //设置面包屑导航
    public function setBreadCrumb($bread_crumb)
    {
        $this->_bread_crumb = $bread_crumb;
        return $this;
    }
    //设置顶部工具栏按钮池
    public function addTopButton($type,$attribute = null)
    {
        switch ($type) {
            case 'add': // 添加新增按钮
                // 预定义按钮属性以简化使用
                $my_attribute['title'] = '添加';
                $my_attribute['class'] = 'btn btn-primary btn-add';
                $my_attribute['href']  = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/add');
                // 自定义属性数组则与默认的进行合并
                if ($attribute && is_array($attribute)) {
                    $my_attribute = array_merge($my_attribute, $attribute);
                }
                // 这个按钮定义好了把它丢进按钮池里
                $this->_top_button_list[] = $my_attribute;
                break;
            case 'resume': // 添加启用按钮(禁用的反操作)
                //预定义按钮属性以简化使用
                $my_attribute['title']       = '批量启用';
                $my_attribute['class']       = 'btn btn-warning btn-resumesome';
                $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus');
                // 自定义属性数组则与默认的进行合并
                if ($attribute && is_array($attribute)) {
                    $my_attribute = array_merge($my_attribute, $attribute);
                }
                // 这个按钮定义好了把它丢进按钮池里
                $this->_top_button_list[] = $my_attribute;
                break;
            case 'forbid': // 添加禁用按钮(启用的反操作)
                //预定义按钮属性以简化使用
                $my_attribute['title']       = '批量禁用';
                $my_attribute['class']       = 'btn btn-warning btn-forbidsome';
                $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus');
                // 自定义属性数组则与默认的进行合并
                if ($attribute && is_array($attribute)) {
                    $my_attribute = array_merge($my_attribute, $attribute);
                }
                // 这个按钮定义好了把它丢进按钮池里
                $this->_top_button_list[] = $my_attribute;
                break;
            case 'delete': // 添加删除按钮(我没有反操作，删除了就没有了，就真的找不回来了)
                //预定义按钮属性以简化使用
                $my_attribute['title']       = '批量删除';
                $my_attribute['class']       = 'btn btn-danger btn-delsome';
                $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/delete');
                // 自定义属性数组则与默认的进行合并
                if ($attribute && is_array($attribute)) {
                    $my_attribute = array_merge($my_attribute, $attribute);
                }
                // 这个按钮定义好了把它丢进按钮池里
                $this->_top_button_list[] = $my_attribute;
                break;
            case 'custom': // 添加自定义按钮
                //预定义按钮属性以简化使用
                $my_attribute['title']       = '自定义';
                $my_attribute['class']       = 'btn btn-primary';
                $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/custom');
                // 自定义属性数组则与默认的进行合并
                if ($attribute && is_array($attribute)) {
                    $my_attribute = array_merge($my_attribute, $attribute);
                }
                // 这个按钮定义好了把它丢进按钮池里
                $this->_top_button_list[] = $my_attribute;
                break;
        }
        return $this;
    }
    //添加一个表格列
    public function addTableColumn($name, $title, $type = null, $width = 'auto', $sort = 'true'){
        $column = array(
            'name'  => $name,
            'title' => $title,
            'type'  => $type,
            'width' => $width,
            'sort' => $sort,
        );
        $this->_table_column_list[] = $column;
        return $this;
    }
    //添加表格数据
    public function setTableDataList($table_data_list){
        $this->_table_data_list = $table_data_list;
        return $this;
    }
    //设置表格操作栏按钮池
    public function addRightButton($type, $attribute = null)
    {
      switch ($type) {
          case 'edit': // 添加编辑按钮
              // 预定义按钮属性以简化使用
              $my_attribute['title'] = '<i class="icon icon-edit"></i> 编辑';
              $my_attribute['class'] = 'btn btn-info btn-xs btn-edit';
              $my_attribute['href']  = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/edit',array('ids'=>'__dataid__'));
              // 自定义属性数组则与默认的进行合并
              if ($attribute && is_array($attribute)) {
                  $my_attribute = array_merge($my_attribute, $attribute);
              }
              // 这个按钮定义好了把它丢进按钮池里
              $this->_right_button_list[] = $my_attribute;
              break;
          case 'resume': // 添加启用按钮(禁用的反操作)
              //预定义按钮属性以简化使用
              $my_attribute['title']       = '<i class="icon icon-check"></i> 启用';
              $my_attribute['class']       = 'btn btn-warning btn-xs btn-resumeone';
              $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',array('ids'=>'__dataid__'));
              // 自定义属性数组则与默认的进行合并
              if ($attribute && is_array($attribute)) {
                  $my_attribute = array_merge($my_attribute, $attribute);
              }
              // 这个按钮定义好了把它丢进按钮池里
              $this->_right_button_list[] = $my_attribute;
              break;
          case 'forbid': // 添加禁用按钮(启用的反操作)
              //预定义按钮属性以简化使用
              $my_attribute['title']       = '<i class="icon icon-times"></i> 禁用';
              $my_attribute['class']       = 'btn btn-warning btn-xs btn-forbidone';
              $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/setStatus',array('ids'=>'__dataid__'));
              // 自定义属性数组则与默认的进行合并
              if ($attribute && is_array($attribute)) {
                  $my_attribute = array_merge($my_attribute, $attribute);
              }
              // 这个按钮定义好了把它丢进按钮池里
              $this->_right_button_list[] = $my_attribute;
              break;
          case 'delete': // 添加删除按钮(我没有反操作，删除了就没有了，就真的找不回来了)
              //预定义按钮属性以简化使用
              $my_attribute['title']       = '<i class="icon icon-trash"></i> 删除';
              $my_attribute['class']       = 'btn btn-danger btn-xs btn-delone';
              $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/delete',array('ids'=>'__dataid__'));
              // 自定义属性数组则与默认的进行合并
              if ($attribute && is_array($attribute)) {
                  $my_attribute = array_merge($my_attribute, $attribute);
              }
              // 这个按钮定义好了把它丢进按钮池里
              $this->_right_button_list[] = $my_attribute;
              break;
          case 'custom': // 添加删除按钮(我没有反操作，删除了就没有了，就真的找不回来了)
              //预定义按钮属性以简化使用
              $my_attribute['title']       = '自定义';
              $my_attribute['class']       = 'btn btn-primary btn-xs';
              $my_attribute['href']        = U(MODULE_NAME . '/' . CONTROLLER_NAME . '/custom');
              // 自定义属性数组则与默认的进行合并
              if ($attribute && is_array($attribute)) {
                  $my_attribute = array_merge($my_attribute, $attribute);
              }
              // 这个按钮定义好了把它丢进按钮池里
              $this->_right_button_list[] = $my_attribute;
              break;
      }
      return $this;
    }
    //设置分页
    public function setTableDataPage($table_data_page)
    {
        $this->_table_data_page = $table_data_page;
        return $this;
    }
    //设置页面模版
    public function setTemplate($template)
    {
        $this->_template = $template;
        return $this;
    }

    //显示页面
    public function fetch()
    {
        foreach ($this->_table_data_list as &$data) {
            // 编译表格右侧按钮
            if ($this->_right_button_list) {
                foreach ($this->_right_button_list as $right_button) {
                    // 将约定的标记__data_id__替换成真实的数据ID
                    $right_button['href'] = str_replace(
                        '__dataid__',
                        $data['id'],
                        $right_button['href']
                    );

                    // 编译按钮属性
                    $data['right_button'][] = $right_button;
                }
            }

            foreach ($this->_table_column_list as &$column) {
                switch ($column['type']) {
                    case 'status':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<i class="icon icon-times text-danger"></i>';
                                break;
                            case '1':
                                $data[$column['name']] = '<i class="icon icon-check text-success"></i>';
                                break;
                        }
                        break;
                    case 'status1':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<i class="icon icon-info-sign text-muted" title="默认"></i>';
                                break;
                            case '1':
                                $data[$column['name']] = '<i class="icon icon-check text-success" title="通过"></i>';
                                break;
                            case '2':
                                $data[$column['name']] = '<i class="icon icon-times text-danger" title="失败"></i>';
                                break;
                        }
                        break;
                    case 'swicth':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<span class="text-muted">否</span>';
                                break;
                            case '1':
                                $data[$column['name']] = '<span class="text-success">是</span>';
                                break;
                        }
                        break;
                    case 'size':
                        $data[$column['name']] = formatBytes($data[$column['name']]);
                    case 'text':
                        if (!$data[$column['name']]) {
                            $data[$column['name']] = '<span class="text-muted">（空）</span>';
                        }else {
                            $data[$column['name']] = '<div class="text-ellipsis" title="'.$data[$column['name']].'">'.$data[$column['name']].'</div>';
                        }
                        break;
                    case 'picture':
                        $path = getImgpath($data[$column['name']]);
                        $data[$column['name']] = '<img class="img-zoomify" src="'.$path.'" style="width:40px"/>';
                        break;
                    case 'image':
                        $path = getImgidpath($data[$column['name']]);
                        $data[$column['name']] = '<img class="img-zoomify" src="'.$path.'" style="height:40px;"/>';
                        break;
                    case 'avatar':
                        $path = getImgidpath($data[$column['name']]);
                        $data[$column['name']] = '<img class="img-circle img-zoomify" src="'.$path.'" style="height:30px;width:30px"/>';
                        break;
                    case 'getteamavatar':
                        $path = getteamavatar($data[$column['name']]);
                        $data[$column['name']] = '<img class="img-zoomify" src="'.$path.'" style="height:30px;width:30px"/>';
                        break;
                    case 'date':
                        if ($data[$column['name']]) {
                            $data[$column['name']] = date('Y-m-d H:i',$data[$column['name']]);
                        }else {
                            $data[$column['name']] = '<span class="text-muted">（空）</span>';
                        }
                        break;
                    case 'date-ymd':
                        if ($data[$column['name']]) {
                            $data[$column['name']] = date('Y-m-d',$data[$column['name']]);
                        }else {
                            $data[$column['name']] = '<span class="text-muted">（空）</span>';
                        }
                        break;
                    case 'getnavup':
                        $data[$column['name']] = getnavup($data[$column['name']]);
                        break;
                    case 'getcolumnup':
                        $data[$column['name']] = getcolumnup($data[$column['name']]);
                        break;
                    case 'getsectionup':
                        $data[$column['name']] = getsectionup($data[$column['name']]);
                        break;
                    case 'getteam':
                        $data[$column['name']] = getteam($data[$column['name']]);
                        break;
                    case 'getcompany':
                        $data[$column['name']] = getcompany($data[$column['name']]);
                        break;
                    case 'getmanage':
                        $data[$column['name']] = getmanage($data[$column['name']]);
                        break;
                    case 'getuser':
                        $data[$column['name']] = getuser($data[$column['name']]);
                        break;
                    case 'getgrade':
                        $data[$column['name']] = getusergrade($data[$column['name']]);
                        break;
                    case 'gettitle':
                        $data[$column['name']] = getusertitle($data[$column['name']]);
                        break;
                    case 'getbugtype':
                        $data[$column['name']] = getbugtype($data[$column['name']]);
                        break;
                    case 'getprojecttype':
                        $data[$column['name']] = getprojecttype($data[$column['name']]);
                        break;
                    case 'buglevel':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<span>默认</span>';
                                break;
                            case '1':
                                $data[$column['name']] = '<span>低危</span>';
                                break;
                            case '2':
                                $data[$column['name']] = '<span>中危</span>';
                                break;
                            case '2':
                                $data[$column['name']] = '<span>高危</span>';
                                break;
                        }
                        break;
                    case 'smstype':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<span>普通短信</span>';
                                break;
                            case '1':
                                $data[$column['name']] = '<span>营销短信</span>';
                                break;
                            case '2':
                                $data[$column['name']] = '<span>语音短信</span>';
                                break;
                        }
                        break;
                    case 'smsstatus':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<span class="text-success">已通过</span>';
                                break;
                            case '1':
                                $data[$column['name']] = '<span class="text-muted">审核中</span>';
                                break;
                            case '2':
                                $data[$column['name']] = '<span class="text-danger">已拒绝</span>';
                                break;
                        }
                        break;
                    case 'projectstatus':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '<span class="text-danger">不通过</span>';
                                break;
                            case '1':
                                $data[$column['name']] = '<span class="text-success">进行中</span>';
                                break;
                            case '2':
                                $data[$column['name']] = '<span class="text-primary">暂停中</span>';
                                break;
                            case '3':
                                $data[$column['name']] = '<span class="text-muted">已结束</span>';
                                break;
                        }
                        break;
                    case 'withdrawtype':
                        switch ($data[$column['name']]) {
                            case '0':
                                $data[$column['name']] = '支付宝';
                                break;
                            case '1':
                                $data[$column['name']] = '银行卡';
                                break;
                            case '2':
                                $data[$column['name']] = '其他';
                                break;
                        }
                        break;
                }
            }
        }

        $this->assign('meta_title',$this->_meta_title);
        $this->assign('bread_crumb',$this->_bread_crumb);
        $this->assign('top_button_list', $this->_top_button_list);
        $this->assign('table_column_list', $this->_table_column_list);
        $this->assign('table_data_list', $this->_table_data_list);
        $this->assign('table_data_page', $this->_table_data_page);
        $this->assign('right_button_list', $this->_right_button_list);
        $this->display($this->_template);
    }
}
